
package com.hlkj.pay.app.merchant.ext.impl.haike;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.framework.extension.annotation.Extension;
import com.hlkj.pay.app.merchant.ext.ChannelMerchantAccessExtPt;
import com.hlkj.pay.app.merchant.ext.MerchantExtConstant;
import com.hlkj.pay.app.merchant.ext.dto.access.*;
import com.hlkj.pay.app.merchant.ext.impl.haike.constant.HaikePayConstant;
import com.hlkj.pay.app.merchant.ext.impl.haike.dto.HaikeAccessAuthDto;
import com.hlkj.pay.app.merchant.ext.impl.haike.dto.HaikePayAppChannelParams;
import com.hlkj.pay.app.merchant.ext.impl.haike.dto.HaikePayConfig;
import com.hlkj.pay.app.merchant.ext.impl.haike.dto.resp.*;
import com.hlkj.pay.app.merchant.ext.impl.haike.handler.HaikePayCommonParamsHandle;
import com.hlkj.pay.app.merchant.ext.impl.leshua.constant.XmlUtils;
import com.hlkj.pay.app.merchant.impl.MerchantAccessStatusProcessHandler;
import com.hlkj.pay.dto.merchant.*;
import com.hlkj.pay.dto.pay.PayCommonAppDto;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.enums.PayEnums;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAccessAuthDO;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAccessInfoDO;
import com.hlkj.pay.infrastructure.model.pay.PayChannelRequestDO;
import com.hlkj.pay.infrastructure.model.pay.ProviderChannelConfigDO;
import com.hlkj.pay.vo.order.resp.MerchantProductQueryResp;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.StringPool;

/**
 * 海科进件操作
 */
@Slf4j
@Repository
@RequiredArgsConstructor
@Extension(bizId = MerchantExtConstant.CHANNEL_HAIKE_PAY)
public class HaikePayMerchantAccessExtPt implements ChannelMerchantAccessExtPt {

    private final HaikePayCommonParamsHandle haikePayCommonParamsHandle;

    private final MerchantAccessStatusProcessHandler merchantAccessStatusProcessHandler;

    @Value("${hlkj.domain}")
    private String domain;

    @Override
    public void channelConfigCheck(ChannelMerchantAccessCommonContext channelMerchantAccessCommonContext) {

    }

    @Override
    public void merchantAccess(ChannelMerchantAccessContext channelMerchantAccessContext) {
        log.info("海科渠道提交进件 请求参数:{}", JsonUtils.toJsonString(channelMerchantAccessContext));
        CommonResult<HaikePayConfig> yeePayConfigCommonResult = haikePayCommonParamsHandle.haikePayConfig(channelMerchantAccessContext.getProviderChannelConfig(),
                channelMerchantAccessContext.isTest());
        if (!yeePayConfigCommonResult.isSuccess()) {
            channelMerchantAccessContext.setRemark(yeePayConfigCommonResult.getMsg());
            channelMerchantAccessContext.setProcessStatus(false);
            return;
        }
        MerchantAccessDto merchantAccessDto = channelMerchantAccessContext.getMerchantAccessDto();
        MerchantAccessInfoDO merchantAccessInfo = merchantAccessDto.getMerchantAccessInfo();
        // 初始化海科配置
        HaikePayConfig haikePayConfig = yeePayConfigCommonResult.getData();
        // 企业类型
        MerchantEnum.MERCHANT_TYPE merchantType = MerchantEnum.MERCHANT_TYPE.from(merchantAccessInfo.getType());

        PayChannelRequestDO payChannelRequestDO = new PayChannelRequestDO();
        channelMerchantAccessContext.setPayChannelRequestDO(payChannelRequestDO);

        try {

            // 更新申请单号
            String agent_apply_no = IdUtil.getSnowflakeNextIdStr();
            merchantAccessInfo.setApplyNo(agent_apply_no);

            // 请求参数
            Map<String,Object> data = new HashMap<>();
            data.put("agent_no",haikePayConfig.getFtpName());
            data.put("agent_apply_no",agent_apply_no);

            // 判断是否修改 启用修改功能时传递
            if(StrUtil.isNotEmpty(merchantAccessInfo.getSubMchId())){
                data.put("merch_no",merchantAccessInfo.getSubMchId());
            }
            // 图片请求参数
            Map<String,Object> image_data = new HashMap<>();

            // 商户基本信息
            Map<String,Object> merchant_data = new HashMap<>();
            merchant_data.put("merch_short_name",merchantAccessDto.getMerchantAccessBusiness().getShortName());

            if(merchantType == MerchantEnum.MERCHANT_TYPE.COMPANY){
                merchant_data.put("merch_name",merchantAccessDto.getMerchantAccessLicense().getName());
                merchant_data.put("merch_type",haikePayCommonParamsHandle.companyType(Integer.valueOf(merchantAccessDto.getMerchantAccessLicense().getCompanyType()),merchantType));
            }else{
                merchant_data.put("merch_name",merchantAccessDto.getMerchantAccessLegal().getName());
                merchant_data.put("merch_type","10C");
            }
            merchant_data.put("shop_name", merchantAccessDto.getMerchantAccessBusiness().getShortName());

            // 营业执照信息  企业必传
            if(merchantType == MerchantEnum.MERCHANT_TYPE.COMPANY){
                merchant_data.put("merchant_cert_type",haikePayCommonParamsHandle.licenseType(merchantAccessDto.getMerchantAccessLicense().getLicenseType()));
                merchant_data.put("bus_license_no",merchantAccessDto.getMerchantAccessLicense().getLicenseCode());
                merchant_data.put("bus_license_start",merchantAccessDto.getMerchantAccessLicense().getLicenseBeginDate());
                if(merchantAccessDto.getMerchantAccessLicense().getLicenseValidityType() == 1){
                    merchant_data.put("bus_license_end","9999-12-31");
                }else{
                    merchant_data.put("bus_license_end",merchantAccessDto.getMerchantAccessLicense().getLicenseEndDate());
                }
                // 执照经营地址
                merchant_data.put("bus_province_code",merchantAccessDto.getMerchantAccessLicense().getProvinceCode());
                merchant_data.put("bus_city_code",merchantAccessDto.getMerchantAccessLicense().getCityCode());
                merchant_data.put("bus_area_code",merchantAccessDto.getMerchantAccessLicense().getAreaCode());
                merchant_data.put("bus_address",merchantAccessDto.getMerchantAccessLicense().getAddress());

                // 执照图片id
                image_data.put("A1",haikePayCommonParamsHandle.getUploadMerQualUrl("执照图片id A1",merchantAccessDto.getMerchantAccessLicense().getLicenseUrl(),haikePayConfig));
            }else{
                // 经营场景类的其他的文件 (合作协议)
                List<String> fileUrls = merchantAccessDto.getMerchantAccessBusiness().getFileUrls();
                if(CollUtil.isEmpty(fileUrls) || fileUrls.size() < 2){
                    // 直接结束
                    channelMerchantAccessContext.setRemark("小微商户合作协议（首页）和（盖章页）未上传");
                    channelMerchantAccessContext.setProcessStatus(false);
                    return;
                }
                // 合作协议（首页）图片id
                image_data.put("A7",haikePayCommonParamsHandle.getUploadMerQualUrl("合作协议（首页）图片id A7",fileUrls.get(0),haikePayConfig));
                // 合作协议（盖章页）图片id
                image_data.put("A9",haikePayCommonParamsHandle.getUploadMerQualUrl("合作协议（盖章页）图片id A9",fileUrls.get(1),haikePayConfig));
            }

            // 实际经营地址
            merchant_data.put("province_code",merchantAccessDto.getMerchantAccessBusiness().getProvinceCode());
            merchant_data.put("city_code",merchantAccessDto.getMerchantAccessBusiness().getCityCode());
            merchant_data.put("area_code",merchantAccessDto.getMerchantAccessBusiness().getAreaCode());
            merchant_data.put("address",merchantAccessDto.getMerchantAccessBusiness().getAddress());

            // 商户门头和室内
            image_data.put("A5",haikePayCommonParamsHandle.getUploadMerQualUrl("A5",merchantAccessDto.getMerchantAccessBusiness().getStoreHeaderUrl(),haikePayConfig));
            image_data.put("A6",haikePayCommonParamsHandle.getUploadMerQualUrl("A6",merchantAccessDto.getMerchantAccessBusiness().getStoreIndoorUrl(),haikePayConfig));
            image_data.put("A31",haikePayCommonParamsHandle.getUploadMerQualUrl("A31",merchantAccessDto.getMerchantAccessBusiness().getStoreCashierDeskUrl(),haikePayConfig));

            // 法人信息
            merchant_data.put("legal_name",merchantAccessDto.getMerchantAccessLegal().getName());
            merchant_data.put("legal_cert_type",haikePayCommonParamsHandle.certType(merchantAccessDto.getMerchantAccessLegal().getCertType()));
            merchant_data.put("legal_cert_no",merchantAccessDto.getMerchantAccessLegal().getCertNo());
            merchant_data.put("legal_cert_start",merchantAccessDto.getMerchantAccessLegal().getCertBeginDate());
            // 法人身份证正面图片id
            image_data.put("A2",haikePayCommonParamsHandle.getUploadMerQualUrl("法人身份证正面图片id A2",merchantAccessDto.getMerchantAccessLegal().getCertFrontUrl(),haikePayConfig));
            // 法人身份证反面图片id
            image_data.put("A3",haikePayCommonParamsHandle.getUploadMerQualUrl("法人身份证反面图片id A3",merchantAccessDto.getMerchantAccessLegal().getCertBackUrl(),haikePayConfig));

            if(merchantAccessDto.getMerchantAccessLegal().getCertValidityType() == 1){
                // merchant_data.put("legal_cert_end","9999-12-31");
            }else{
                merchant_data.put("legal_cert_end",merchantAccessDto.getMerchantAccessLegal().getCertEndDate());
            }

            // 商户联系人信息 联系人非同法
            if(merchantAccessDto.getMerchantAccessInfo().getContactType() != null && merchantAccessDto.getMerchantAccessInfo().getContactType() == 0){
                merchant_data.put("contact_name",merchantAccessDto.getMerchantAccessLegal().getName());
                merchant_data.put("contact_phone",merchantAccessDto.getMerchantAccessLegal().getPhone());
            }else{
                merchant_data.put("contact_name",merchantAccessDto.getMerchantAccessContact().getName());
                merchant_data.put("contact_phone",merchantAccessDto.getMerchantAccessContact().getPhone());
            }

            // 营业范围代码
            merchant_data.put("bus_scope_code",merchantAccessDto.getMerchantAccessBusiness().getMcc());
            data.put("merchant_data",merchant_data);

            // 结算信息
            Map<String,Object> bankcard_data = new HashMap<>();
            bankcard_data.put("acc_no",merchantAccessDto.getMerchantAccessCard().getCardNo());
            bankcard_data.put("bank_code",merchantAccessDto.getMerchantAccessCard().getBranchCode());
            bankcard_data.put("acc_name",merchantAccessDto.getMerchantAccessCard().getName());
            bankcard_data.put("bank_province_code",merchantAccessDto.getMerchantAccessCard().getProvinceCode());
            bankcard_data.put("bank_city_code",merchantAccessDto.getMerchantAccessCard().getCityCode());
            // 是否对公
            boolean corporate = false;
            // 对公/对私
            switch (MerchantEnum.CARD_TYPE.from(merchantAccessDto.getMerchantAccessCard().getType())){
                // 法人结算
                case CORPORATE_ACCOUNT:
                    corporate = true;
                    bankcard_data.put("acc_type","10A");
                    bankcard_data.put("bank_name",merchantAccessDto.getMerchantAccessCard().getBankName());
                    image_data.put("A4",haikePayCommonParamsHandle.getUploadMerQualUrl("A4",merchantAccessDto.getMerchantAccessCard().getLicenseUrl(),haikePayConfig));
                    break;
                // 对私法人结算
                case PRIVATE_CORPORATE_ACCOUNT:
                    bankcard_data.put("acc_type","10B");
                    bankcard_data.put("idcard_no",merchantAccessDto.getMerchantAccessLegal().getCertNo());
                    bankcard_data.put("phone",merchantAccessDto.getMerchantAccessCard().getCardPhone());
                    image_data.put("A4",haikePayCommonParamsHandle.getUploadMerQualUrl("A4",merchantAccessDto.getMerchantAccessCard().getSettleCardFrontUrl(),haikePayConfig));
                    break;
                // 对私非法人结算
                case PRIVATE_NON_CORPORATE_ACCOUNT:
                    bankcard_data.put("acc_type","10B");
                    bankcard_data.put("idcard_no",merchantAccessDto.getMerchantAccessCard().getCertNo());
                    bankcard_data.put("phone",merchantAccessDto.getMerchantAccessCard().getCardPhone());
                    image_data.put("A4",haikePayCommonParamsHandle.getUploadMerQualUrl("A4",merchantAccessDto.getMerchantAccessCard().getSettleCardFrontUrl(),haikePayConfig));
                    // 非法人结算必填
                    image_data.put("A9999",haikePayCommonParamsHandle.getUploadMerQualUrl("A9999",merchantAccessDto.getMerchantAccessCard().getAuthUrl(),haikePayConfig));
                    image_data.put("A35",haikePayCommonParamsHandle.getUploadMerQualUrl("A35",merchantAccessDto.getMerchantAccessCard().getCertFrontUrl(),haikePayConfig));
                    image_data.put("A36",haikePayCommonParamsHandle.getUploadMerQualUrl("A36",merchantAccessDto.getMerchantAccessCard().getCertBackUrl(),haikePayConfig));
                    // 法人手持结算授权函合影图片id
                    image_data.put("A32",haikePayCommonParamsHandle.getUploadMerQualUrl("法人手持结算授权函合影图片id A32",merchantAccessDto.getMerchantAccessCard().getSettleCertFrontUrl(),haikePayConfig));
                    break;
            }
            data.put("bankcard_data",bankcard_data);

            // 图片上传信息
            data.put("image_data",image_data);

            // 支付产品信息
            Map<String,Object> product_data = new HashMap<>();
            // 微信业务申请信息
            Map<String,Object> product_data_wx = new HashMap<>();
            product_data_wx.put("channel_no",haikePayConfig.getWxChannelId());
            product_data_wx.put("rate",haikePayCommonParamsHandle.getRateUnit(channelMerchantAccessContext.getMerchantFeeConfigList(),PayEnums.PAY_TYPE.WECHAT,PayEnums.SCENE_TYPE.OFFLINE,false));
            product_data.put("wx",product_data_wx);
            // 支付宝业务申请信息
            Map<String,Object> product_data_ali = new HashMap<>();
            product_data_ali.put("channel_no",haikePayConfig.getAliPid());
            product_data_ali.put("rate",haikePayCommonParamsHandle.getRateUnit(channelMerchantAccessContext.getMerchantFeeConfigList(),PayEnums.PAY_TYPE.ALIPAY,PayEnums.SCENE_TYPE.OFFLINE,false));
            product_data.put("ali",product_data_ali);
            // 银行卡/银联二维码业务申请信息
            Map<String,Object> product_data_other= new HashMap<>();
            product_data_other.put("bank_debit_rate",41);
            product_data_other.put("bank_debit_max",20*100);
            product_data_other.put("bank_credit_rate",52);
            // 银联
            product_data_other.put("union_debit_rate",haikePayCommonParamsHandle.getRateUnit(channelMerchantAccessContext.getMerchantFeeConfigList(),PayEnums.PAY_TYPE.UNION_APY,PayEnums.SCENE_TYPE.OFFLINE,true));
            product_data_other.put("union_debit_max",20*100);
            product_data_other.put("union_credit_rate",haikePayCommonParamsHandle.getRateUnit(channelMerchantAccessContext.getMerchantFeeConfigList(),PayEnums.PAY_TYPE.UNION_APY,PayEnums.SCENE_TYPE.OFFLINE,true));
            product_data_other.put("union_mix_rate",haikePayCommonParamsHandle.getRateUnit(channelMerchantAccessContext.getMerchantFeeConfigList(),PayEnums.PAY_TYPE.UNION_APY,PayEnums.SCENE_TYPE.OFFLINE,false));
            product_data.put("bank",product_data_other);
            data.put("product_data",product_data);

            // 结算产品信息
            data.put("settle_data",haikePayCommonParamsHandle.getSettleProductInfo(channelMerchantAccessContext.getMerchantFeeConfigList(),corporate));

            // 记录请求参数
            payChannelRequestDO.setRequestId(merchantAccessInfo.getRequestId());
            payChannelRequestDO.setRequestParams(JsonUtils.toJsonString(data));
            payChannelRequestDO.setRequestUrl(HaikePayConstant.REGISTER_URL);
            payChannelRequestDO.setRequestTime(System.currentTimeMillis());

            // 海科驳回 走修改接口
            /**
             * 自动审核通过、3
             * 审核中、4
             * 审核通过、7
             * 运营审核退回 、8
             * 运营审核退回未处理完 9
             */
            String response = null;
            if(StrUtil.isNotEmpty(merchantAccessInfo.getSubMchId())){
                // 只需要传 merchant_data和image_data
                log.info("海科"+merchantType.getDesc()+"开户 请求修改参数 request:{}", JsonUtils.toJsonString(data));
                response = haikePayCommonParamsHandle.requestPostApi(HaikePayConstant.REGISTER_UPDATE_URL,data,haikePayConfig);
                log.info("海科"+merchantType.getDesc()+"开户 返回修改参数 response:{}", JsonUtils.toJsonString(response));
            }else{
                log.info("海科"+merchantType.getDesc()+"开户 请求参数 request:{}", JsonUtils.toJsonString(data));
                response = haikePayCommonParamsHandle.requestPostApi(HaikePayConstant.REGISTER_URL,data,haikePayConfig);
                log.info("海科"+merchantType.getDesc()+"开户 返回参数 response:{}", JsonUtils.toJsonString(response));
            }

            payChannelRequestDO.setResponse(response);
            // 参数转化
            HaikeAccessResp accessResp = JSONUtil.toBean(response,HaikeAccessResp.class);

            if(HaikePayConstant.OK.equals(accessResp.getReturn_code())){
                // 修改时返回申请单号
                if(StrUtil.isNotEmpty(accessResp.getApply_id())){
                    // 判断是否修改
                    merchantAccessInfo.setExtendA(accessResp.getApply_id());
                    merchantAccessInfo.setApplyNo(accessResp.getApply_id());
                }

                if(StrUtil.isNotEmpty(accessResp.getMerch_no())){
                    merchantAccessInfo.setSubMchId(accessResp.getMerch_no());
                    // 获取终端号
                    String terminalNo = haikePayCommonParamsHandle.getTerminalNo(merchantAccessInfo,haikePayConfig);
                    if(StrUtil.isNotEmpty(terminalNo)){
                        merchantAccessInfo.setExtendB(terminalNo);
                    }
                }
                // 下一步就是查询申请状态
                channelMerchantAccessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.CHANNEL_APPROVE);
                channelMerchantAccessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.SUBMIT);
            }else{
                channelMerchantAccessContext.setRemark(accessResp.getReturn_msg());
                channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
                channelMerchantAccessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
            }

        } catch (Exception e) {
            log.error("海科开户请求失败,失败信息 message: {}", e);
            channelMerchantAccessContext.setRemark("海科开户请求失败,失败信息, " + e.getMessage());
            channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
            channelMerchantAccessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
        }

    }

    @Override
    public String requestUrl(MerchantAccessDto merchantAccessInfoDO) {
        return null;
    }

    @Override
    public String channelParams(AccessChannelParamsDto accessChannelParamsDto) {
        HaikePayAppChannelParams params = new HaikePayAppChannelParams();
        params.setSubMchId(accessChannelParamsDto.getSubMchId());
        params.setPn(accessChannelParamsDto.getExtendB());
        return JsonUtils.toJsonStringNotNull(params);
    }

    @Override
    public void merchantAccessQuery(ChannelMerchantAccessQueryContext channelMerchantAccessQueryContext) {

        log.info("海科进件查询请求参数:{}", JsonUtils.toJsonString(channelMerchantAccessQueryContext));
        CommonResult<HaikePayConfig> haikePayConfigCommonResult = haikePayCommonParamsHandle.haikePayConfig(channelMerchantAccessQueryContext.getProviderChannelConfig(),
                channelMerchantAccessQueryContext.isTest());
        if (!haikePayConfigCommonResult.isSuccess()) {
            channelMerchantAccessQueryContext.setRemark(haikePayConfigCommonResult.getMsg());
            channelMerchantAccessQueryContext.setProcessStatus(false);
            return;
        }
        // 进件信息
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantAccessQueryContext.getMerchantAccessInfo();
        // 海科配置信息
        HaikePayConfig haikePayConfig = haikePayConfigCommonResult.getData();
        try{
            Map<String,Object> data = new HashMap<>();
            data.put("agent_no",haikePayConfig.getFtpName());
            String response = null;
            String status = null;
            String failMsg = null;
            // 判断海科修改进件查询
            if(StrUtil.isEmpty(merchantAccessInfo.getExtendA())){
                String merchantId = merchantAccessInfo.getSubMchId();
                if(StrUtil.isEmpty(merchantId)){
                    channelMerchantAccessQueryContext.setRemark("海科查询进件 merchantId is null");
                    channelMerchantAccessQueryContext.setProcessStatus(false);
                    return;
                }
                data.put("agent_apply_no",merchantAccessInfo.getApplyNo());
                data.put("merch_no",merchantId);
                response = haikePayCommonParamsHandle.requestPostApi(HaikePayConstant.AUDIT_QUERY_URL,data,haikePayConfig);
                log.info("海科查询进件 返回参数:{}", response);
                // 返回参数
                HaikeAccessQueryResp accessQueryResp = JSONUtil.toBean(response,HaikeAccessQueryResp.class);
                if(!HaikePayConstant.OK.equals(accessQueryResp.getReturn_code().toString())){
                    channelMerchantAccessQueryContext.setProcessStatus(false);
                    channelMerchantAccessQueryContext.setRemark(accessQueryResp.getReturn_msg());
                    return;
                }
                status = accessQueryResp.getStatus();
                failMsg = accessQueryResp.getFail_msg();
            }else{
                data.put("apply_no",merchantAccessInfo.getApplyNo());
                response = haikePayCommonParamsHandle.requestPostApi(HaikePayConstant.AUDIT_UPDATE_QUERY_URL,data,haikePayConfig);
                log.info("海科修改查询进件 返回参数:{}", response);
                HaikeAccessUpdateQueryResp accessQueryResp = JSONUtil.toBean(response,HaikeAccessUpdateQueryResp.class);
                if(!HaikePayConstant.OK.equals(accessQueryResp.getReturn_code().toString())){
                    channelMerchantAccessQueryContext.setProcessStatus(false);
                    channelMerchantAccessQueryContext.setRemark(accessQueryResp.getReturn_msg());
                    return;
                }
                status = accessQueryResp.getAccept_status();
                failMsg = accessQueryResp.getAccept_fail_msg();
            }

            /**
             * 审核状态转换
             * 系统审核状态 0：草稿 1：鉴权中 2：已提交，等待平台审核，3：系统自动审核中，4：系统审核通过渠道审核中，5：平台审核中，6：待签约，7：签约中，8：待配置，9：配置中，10：审核完成，11：审核驳回
             * 海科进件审核状态如下：
             * 1:提交失败
             * 2:已受理
             * 3:自动审核
             * 4:待审核
             * 5:审核失败
             * 6:审核拒绝
             * 7:审核成功
             * 8:运营审核退回
             * 9:运营审核退回未处理完
             */
            switch (status) {
                case "1":
                case "5":
                case "6":
                case "8":
                case "9":
                case "fail":
                    channelMerchantAccessQueryContext.setProcessStatus(false);
                    channelMerchantAccessQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                    channelMerchantAccessQueryContext.setRemark(failMsg);
                    break;
                case "2":
                case "audit":
                    channelMerchantAccessQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.CHANNEL_APPROVE);
                    channelMerchantAccessQueryContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.CONTINUE_APPLY_QUERY);
                    // 终端号查询
                    if(StrUtil.isEmpty(merchantAccessInfo.getExtendB())){
                        // 获取终端号
                        String terminalNo = haikePayCommonParamsHandle.getTerminalNo(merchantAccessInfo,haikePayConfig);
                        if(StrUtil.isNotEmpty(terminalNo)){
                            merchantAccessInfo.setExtendB(terminalNo);
                        }
                    }
                    break;
                case "3":
                case "4":
                case "7":
                case "success":
                    channelMerchantAccessQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_SUCCESS);
                    channelMerchantAccessQueryContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.SUBMIT);

                    // 终端号查询
                    if(StrUtil.isEmpty(merchantAccessInfo.getExtendB())){
                        // 获取终端号
                        String terminalNo = haikePayCommonParamsHandle.getTerminalNo(merchantAccessInfo,haikePayConfig);
                        if(StrUtil.isNotEmpty(terminalNo)){
                            merchantAccessInfo.setExtendB(terminalNo);
                        }
                    }
                    break;
                default:
                    channelMerchantAccessQueryContext.setProcessStatus(false);
                    channelMerchantAccessQueryContext.setRemark(failMsg);
                    break;
            }

        }catch (Exception e){
            log.error("海科查询进件请求失败,失败信息 message: {}", e);
            channelMerchantAccessQueryContext.setProcessStatus(false);
            channelMerchantAccessQueryContext.setRemark(e.getMessage());
        }
    }

    @Override
    public void merchantSign(ChannelMerchantAccessSignContext channelMerchantAccessSignContext) {
    }

    @Override
    public void merchantSignQuery(ChannelMerchantAccessSignContext channelMerchantAccessSignContext) {
    }

    @Override
    public void merchantBusinessOpen(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
    }

    @Override
    public void wechatConfigInit(ChannelMerchantBusinessContext channelMerchantBusinessContext) {

        CommonResult<HaikePayConfig> haikePayConfigCommonResult = haikePayCommonParamsHandle.haikePayConfig(channelMerchantBusinessContext.getProviderChannelConfig(),
                channelMerchantBusinessContext.isTest());
        if (!haikePayConfigCommonResult.isSuccess()) {
            channelMerchantBusinessContext.setRemark(haikePayConfigCommonResult.getMsg());
            channelMerchantBusinessContext.setProcessStatus(false);
            return;
        }
        // 进件信息
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantBusinessContext.getMerchantAccessDto().getMerchantAccessInfo();
        // 海科配置信息
        HaikePayConfig haikePayConfig = haikePayConfigCommonResult.getData();
        try {
            List<PayCommonAppDto> payCommonAppList = channelMerchantBusinessContext.getPayCommonAppList();
            List<PayCommonAppDto> wechatCommonApp = null;
            if (CollUtil.isNotEmpty(payCommonAppList)) {
                wechatCommonApp = payCommonAppList.stream().filter(payCommonAppDto ->  payCommonAppDto.getPayWayDO() !=null && PayEnums.PAY_TYPE.WECHAT.getCode().equals(payCommonAppDto.getPayWayDO().getType()))
                        .collect(Collectors.toList());
            }
            // 没有微信执行 配置完成
            if (CollectionUtils.isEmpty(wechatCommonApp)) {
                // channelMerchantBusinessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.ALIPAY_AUTH);
                return;
            }

            Map<String,Object> data = new HashMap<>();
            data.put("agent_no",haikePayConfig.getFtpName());
            data.put("merch_no",merchantAccessInfo.getSubMchId());

            // 授权目录
            if (!domain.endsWith(StringPool.SLASH)) {
                domain = domain + StringPool.SLASH;
            }
            Map<String,Object> auth_path = MapUtil.newConcurrentHashMap(data);
            auth_path.put("conf_key","auth_path");
            auth_path.put("conf_value",domain);
            log.info("海科 微信参数初始化配置 更新授权目录 请求参数 :{}", JsonUtils.toJsonString(auth_path));
            String response = haikePayCommonParamsHandle.requestPostApi(HaikePayConstant.WX_CONFIG_URL,auth_path,haikePayConfig);
            log.info("海科 微信参数初始化配置 更新授权目录 返回参数 :{}", JsonUtils.toJsonString(response));

            // 支付appId目录列表
            for(PayCommonAppDto commonAppDto:wechatCommonApp){
                Map<String,Object> pay_appId = MapUtil.newConcurrentHashMap(data);
                pay_appId.put("conf_key","pay_appId");
                pay_appId.put("conf_value",commonAppDto.getAppId());
                log.info("海科 微信参数初始化配置 更新支付appid 请求参数 :{}", JsonUtils.toJsonString(pay_appId));
                response = haikePayCommonParamsHandle.requestPostApi(HaikePayConstant.WX_CONFIG_URL,pay_appId,haikePayConfig);
                log.info("海科 微信参数初始化配置 更新支付appid 返回参数 :{}", JsonUtils.toJsonString(response));
            }
        }
        catch (Exception e) {
            log.error("海科微信配置init请求失败,失败信息 message: {}", e);
            channelMerchantBusinessContext.setRemark("海科微信配置init请求失败,失败信息" + e.getMessage());
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
        }

        channelMerchantBusinessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.FINISH);
    }


    @Override
    public void wechatConfigAdd(ChannelMerchantBusinessContext channelMerchantBusinessContext) {

        CommonResult<HaikePayConfig> haikePayConfigCommonResult = haikePayCommonParamsHandle.haikePayConfig(channelMerchantBusinessContext.getProviderChannelConfig(),
                channelMerchantBusinessContext.isTest());
        if (!haikePayConfigCommonResult.isSuccess()) {
            channelMerchantBusinessContext.setRemark(haikePayConfigCommonResult.getMsg());
            channelMerchantBusinessContext.setProcessStatus(false);
            return;
        }
        // 进件信息
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantBusinessContext.getMerchantAccessDto().getMerchantAccessInfo();
        // 海科配置信息
        HaikePayConfig haikePayConfig = haikePayConfigCommonResult.getData();
        // 配置信息
        MerchantWechatConfigDto merchantWechatConfigDto = channelMerchantBusinessContext.getMerchantWechatConfigDto();
        try {
            // 错误消息
            Map<String,String> failMap = new HashMap<>();
            // 参数组装
            Map<String,Object> data = new HashMap<>();
            data.put("agent_no",haikePayConfig.getFtpName());
            data.put("merch_no",merchantAccessInfo.getSubMchId());
            // 支付appId目录列表
            String appId = merchantWechatConfigDto.getAppId();
            if(StrUtil.isNotEmpty(appId)){
                Map<String,Object> pay_appId = MapUtil.newConcurrentHashMap(data);
                pay_appId.put("conf_key","pay_appId");
                pay_appId.put("conf_value",appId);
                log.info("海科 微信参数初始化配置 更新支付appid 请求参数 :{}", JsonUtils.toJsonString(pay_appId));
                String response = haikePayCommonParamsHandle.requestPostApi(HaikePayConstant.WX_CONFIG_URL,pay_appId,haikePayConfig);
                HaikeConfigResp configResp = JSONUtil.toBean(response,HaikeConfigResp.class);
                if(configResp.getResult().equals("1")){
                    failMap.put(appId,configResp.getMsg());
                }
                log.info("海科 微信参数初始化配置 更新支付appid 返回参数 :{}", JsonUtils.toJsonString(response));
            }
            // 支付授权目录列表
            if(StrUtil.isNotEmpty(merchantWechatConfigDto.getJsAPIPath())){
                String jsAPIPath = merchantWechatConfigDto.getJsAPIPath();
                Map<String,Object> auth_path = MapUtil.newConcurrentHashMap(data);
                auth_path.put("conf_key","auth_path");
                auth_path.put("conf_value",jsAPIPath);
                log.info("海科 微信参数初始化配置 更新授权目录 请求参数 :{}", JsonUtils.toJsonString(auth_path));
                String response = haikePayCommonParamsHandle.requestPostApi(HaikePayConstant.WX_CONFIG_URL,auth_path,haikePayConfig);
                HaikeConfigResp configResp = JSONUtil.toBean(response,HaikeConfigResp.class);
                if(configResp.getResult().equals("1")){
                    failMap.put(jsAPIPath,configResp.getMsg());
                }
                log.info("海科 微信参数初始化配置 更新授权目录 返回参数 :{}", JsonUtils.toJsonString(response));
            }

            if(MapUtil.isNotEmpty(failMap)){
                channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
                channelMerchantBusinessContext.setRemark(JSONUtil.toJsonPrettyStr(failMap));
            }
        }
        catch (Exception e) {
            log.error("海科微信配置add请求失败,失败信息 message: {}", e);
            channelMerchantBusinessContext.setRemark("海科微信配置add请求失败,失败信息" + e.getMessage());
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
        }

    }

    @Override
    public void wechatConfigQuery(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {

        CommonResult<HaikePayConfig> haikePayConfigCommonResult = haikePayCommonParamsHandle.haikePayConfig(channelMerchantAccessAuthQueryContext.getProviderChannelConfig(),
                channelMerchantAccessAuthQueryContext.isTest());
        if (!haikePayConfigCommonResult.isSuccess()) {
            channelMerchantAccessAuthQueryContext.setRemark(haikePayConfigCommonResult.getMsg());
            channelMerchantAccessAuthQueryContext.setProcessStatus(false);
            return;
        }
        // 进件信息
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantAccessAuthQueryContext.getMerchantAccessInfo();
        // 海科配置信息
        HaikePayConfig haikePayConfig = haikePayConfigCommonResult.getData();
        try {
            MerchantWechatConfigListDto merchantWechatConfigListDto = new MerchantWechatConfigListDto();
            merchantWechatConfigListDto.setAppIdList(CollUtil.newArrayList());
            merchantWechatConfigListDto.setJsApiPath(CollUtil.newArrayList());

            Map<String,Object> data = new HashMap<>();
            data.put("merch_no",merchantAccessInfo.getSubMchId());
            log.info("海科 微信配置查询 请求参数 :{}", JsonUtils.toJsonString(data));
            String response = haikePayCommonParamsHandle.requestPostApi(HaikePayConstant.WX_CONFIG_QRY_URL,data,haikePayConfig);
            log.info("海科 微信配置查询 返回参数 :{}", JsonUtils.toJsonString(response));
            HaikeConfigQueryResp configQueryResp = JSONUtil.toBean(response,HaikeConfigQueryResp.class);
            if(HaikePayConstant.OK.equals(configQueryResp.getReturn_code()) && configQueryResp.getResult_code().equals("SUCCESS")){
                if(configQueryResp != null){
                    if(CollUtil.isNotEmpty(configQueryResp.getAppid_config_list())){
                        for (HaikeConfigQueryResp.AppidConfigList appIdConfigDetail :configQueryResp.getAppid_config_list()) {
                            MerchantWechatConfigAppIdDto merchantWechatConfigAppIdDto = new MerchantWechatConfigAppIdDto();
                            merchantWechatConfigAppIdDto.setSubAppid(appIdConfigDetail.getSub_appid());
                            merchantWechatConfigAppIdDto.setSubscribeAppid(appIdConfigDetail.getSubscribe_appid());
                            merchantWechatConfigListDto.getAppIdList().add(merchantWechatConfigAppIdDto);
                        }
                    }
                    if(CollUtil.isNotEmpty(configQueryResp.getJsapi_path_list())){
                        for (String jsapi_path : configQueryResp.getJsapi_path_list()) {
                            merchantWechatConfigListDto.getJsApiPath().add(jsapi_path);
                        }
                    }
                }
            }else{
                log.error("海科 微信配置查询 请求失败,失败信息 message: {}", configQueryResp.getReturn_msg());
            }
            channelMerchantAccessAuthQueryContext.setMerchantWechatConfigListDto(merchantWechatConfigListDto);
            // 海科 微信 不需要报备
            channelMerchantAccessAuthQueryContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.FINISH);
        }
        catch (Exception e) {
            log.error("海科微信配置查询请求失败,失败信息 message: {}", e);
            channelMerchantAccessAuthQueryContext.setRemark("海科微信配置查询请求失败,失败信息" + e.getMessage());
            channelMerchantAccessAuthQueryContext.setProcessStatus(Boolean.FALSE);
        }
    }

    @Override
    public void wechatAuthConfig(ChannelMerchantBusinessContext channelMerchantBusinessContext) {


    }

    @Override
    public boolean authConfigQueryType() {
        return false;
    }

    @Override
    public void wechatAuthQuery(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {

        CommonResult<HaikePayConfig> haikePayConfigCommonResult = haikePayCommonParamsHandle.haikePayConfig(channelMerchantAccessAuthQueryContext.getProviderChannelConfig(),
                channelMerchantAccessAuthQueryContext.isTest());
        if (!haikePayConfigCommonResult.isSuccess()) {
            channelMerchantAccessAuthQueryContext.setRemark(haikePayConfigCommonResult.getMsg());
            channelMerchantAccessAuthQueryContext.setProcessStatus(false);
            return;
        }
        // 进件信息
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantAccessAuthQueryContext.getMerchantAccessInfo();
        // 海科配置信息
        HaikePayConfig haikePayConfig = haikePayConfigCommonResult.getData();
        try {

            List<MerchantAccessAuthDO> authDOList = new ArrayList<>();
            MerchantAccessAuthDO merchantAccessAuthDO = new MerchantAccessAuthDO();
            // 查询微信子商户号
            HaikeAccessAuthDto authDto = haikePayCommonParamsHandle.getMchId(haikePayConfig,merchantAccessInfo.getSubMchId(), PayEnums.PAY_TYPE.WECHAT);
            if(authDto !=null && StrUtil.isNotEmpty(authDto.getSubMcuId())){
                merchantAccessAuthDO.setType(PayEnums.PAY_TYPE.WECHAT.getCode());
                merchantAccessAuthDO.setSn(merchantAccessInfo.getSn());
                merchantAccessAuthDO.setAccessId(merchantAccessInfo.getId());
                merchantAccessAuthDO.setMchId(authDto.getChannelNo());
                merchantAccessAuthDO.setSubMchId(authDto.getSubMcuId());
                merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.APPROVE.getCode());
                authDOList.add(merchantAccessAuthDO);
                // 异步更新授权状态
                MerchantAuthStatusConfig merchantAuthStatusConfig = new MerchantAuthStatusConfig();
                merchantAuthStatusConfig.setReportMerchantNo(authDto.getSubMcuId());
                merchantAuthStatusConfig.setAccessId(merchantAccessInfo.getId());
                merchantAuthStatusConfig.setPayType(PayEnums.PAY_TYPE.WECHAT);
                merchantAccessStatusProcessHandler.processAuthAccess(merchantAuthStatusConfig);
            }

            channelMerchantAccessAuthQueryContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.WECHAT_AUTH);
            channelMerchantAccessAuthQueryContext.setMerchantAccessAuthList(authDOList);
        }
        catch (Exception e) {
            log.error("海科微信认证查询请求失败,失败信息 message: {}", e);
            channelMerchantAccessAuthQueryContext.setRemark("海科微信认证查询请求失败,失败信息" + e.getMessage());
            channelMerchantAccessAuthQueryContext.setProcessStatus(Boolean.FALSE);
        }

    }

    @Override
    public void alipayAuthConfig(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
    }

    @Override
    public void alipayAuthQuery(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {

        CommonResult<HaikePayConfig> haikePayConfigCommonResult = haikePayCommonParamsHandle.haikePayConfig(channelMerchantAccessAuthQueryContext.getProviderChannelConfig(),
                channelMerchantAccessAuthQueryContext.isTest());
        if (!haikePayConfigCommonResult.isSuccess()) {
            channelMerchantAccessAuthQueryContext.setRemark(haikePayConfigCommonResult.getMsg());
            channelMerchantAccessAuthQueryContext.setProcessStatus(false);
            return;
        }
        // 进件信息
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantAccessAuthQueryContext.getMerchantAccessDto().getMerchantAccessInfo();
        // 海科配置信息
        HaikePayConfig haikePayConfig = haikePayConfigCommonResult.getData();
        try {

            List<MerchantAccessAuthDO> authDOList = new ArrayList<>();
            // 查询支付宝子商户号
            HaikeAccessAuthDto authDto = haikePayCommonParamsHandle.getMchId(haikePayConfig,merchantAccessInfo.getSubMchId(), PayEnums.PAY_TYPE.ALIPAY);
            if(authDto !=null && StrUtil.isNotEmpty(authDto.getSubMcuId())){
                MerchantAccessAuthDO merchantAccessAuthDO = new MerchantAccessAuthDO();
                merchantAccessAuthDO.setType(PayEnums.PAY_TYPE.ALIPAY.getCode());
                merchantAccessAuthDO.setSn(merchantAccessInfo.getSn());
                merchantAccessAuthDO.setAccessId(merchantAccessInfo.getId());
                merchantAccessAuthDO.setMchId(authDto.getChannelNo());
                merchantAccessAuthDO.setSubMchId(authDto.getSubMcuId());
                merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.APPROVE.getCode());
                authDOList.add(merchantAccessAuthDO);
                // 异步更新授权状态
                MerchantAuthStatusConfig merchantAuthStatusConfig = new MerchantAuthStatusConfig();
                merchantAuthStatusConfig.setReportMerchantNo(authDto.getSubMcuId());
                merchantAuthStatusConfig.setAccessId(merchantAccessInfo.getId());
                merchantAuthStatusConfig.setPayType(PayEnums.PAY_TYPE.ALIPAY);
                merchantAccessStatusProcessHandler.processAuthAccess(merchantAuthStatusConfig);
            }
            channelMerchantAccessAuthQueryContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.ALIPAY_AUTH);
            channelMerchantAccessAuthQueryContext.setMerchantAccessAuthList(authDOList);
        }
        catch (Exception e) {
            log.error("海科支付宝认证查询请求失败,失败信息 message: {}", e);
            channelMerchantAccessAuthQueryContext.setRemark("海科支付宝认证查询请求失败,失败信息" + e.getMessage());
            channelMerchantAccessAuthQueryContext.setProcessStatus(Boolean.FALSE);
        }

    }

    @Override
    public MerchantAccessCallBackDto accessCallBackParamsParse(HttpServletRequest request, ProviderChannelConfigDO providerChannelConfigDO) {

        CommonResult<HaikePayConfig> haikePayConfigCommonResult = haikePayCommonParamsHandle.haikePayConfig(providerChannelConfigDO,true);

        MerchantAccessCallBackDto merchantAccessCallBackDto = new MerchantAccessCallBackDto();
        if (haikePayConfigCommonResult.isSuccess()) {
            HaikePayConfig haikePayConfig = haikePayConfigCommonResult.getData();
            try{
                // 获取响应内容
                String response = XmlUtils.getContentFromRequestInputStream(request);
                HaikeAccessNotify authNotify = JSONUtil.toBean(response,HaikeAccessNotify.class);
                log.info("海科收到回调 response:{}",response);
                switch (authNotify.getStatus()) {
                    case "1":
                    case "5":
                    case "6":
                    case "8":
                    case "9":
                        merchantAccessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                        merchantAccessCallBackDto.setRemark(authNotify.getMsg());
                        break;
                    case "2":
                    case "3":
                    case "4":
                        merchantAccessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.CHANNEL_APPROVE);
                        break;
                    case "7":
                        merchantAccessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_SUCCESS);
                        merchantAccessCallBackDto.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.SUBMIT);
                        break;
                    default:
                        merchantAccessCallBackDto.setProcessStatus(false);
                        merchantAccessCallBackDto.setRemark(authNotify.getMsg());
                        break;
                }
                merchantAccessCallBackDto.setSubMchId(authNotify.getMerch_no());

            }catch (Exception e){
                log.error("海科收到进件回调解析失败,失败信息 message: {}", e);
                merchantAccessCallBackDto.setProcessStatus(false);
                merchantAccessCallBackDto.setRemark("海科收到回调 plaintTextWithPrivateKey, 解析失败, msg");
            }
        }
        return merchantAccessCallBackDto;
    }

    @Override
    public CommonResult<String> accessCallbackParse(String callBackBody, List<ProviderChannelConfigDO> channelConfigDOList) {
        return null;
    }

    @Override
    public MerchantBusinessCallBackDto businessCallBackParamsParse(String callBackBody, ProviderChannelConfigDO providerChannelConfigDO) {
        return null;
    }

    @Override
    public boolean verify(String callBackBody, ProviderChannelConfigDO providerChannelConfigDO) {
        return false;
    }

    @Override
    public String callBackResponse(CommonResult commonResult) {
        if (commonResult.isSuccess()) {
            return "SUCCESS";
        }
        else {
            return "FAIL";
        }
    }

    @Override
    public void termAdd(ChannelMerchantTermContext channelMerchantTermContext) {
    }

    @Override
    public void reloadMerchantAuth(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
    }

    @Override
    public void channelSplit(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
    }

    @Override
    public void productUpdate(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
    }

    @Override
    public Integer queryMerAuthStatus(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {

        Integer authStatus = null;
        CommonResult<HaikePayConfig> haikePayConfigCommonResult = haikePayCommonParamsHandle.haikePayConfig(channelMerchantAccessAuthQueryContext.getProviderChannelConfig(),
                channelMerchantAccessAuthQueryContext.isTest());
        if (!haikePayConfigCommonResult.isSuccess()) {
            channelMerchantAccessAuthQueryContext.setRemark(haikePayConfigCommonResult.getMsg());
            channelMerchantAccessAuthQueryContext.setProcessStatus(false);
            return authStatus;
        }
        // 进件信息
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantAccessAuthQueryContext.getMerchantAccessDto().getMerchantAccessInfo();
        // 海科配置信息
        HaikePayConfig haikePayConfig = haikePayConfigCommonResult.getData();
        // 授权状态查询配置
        MerchantAuthStatusConfig authStatusConfig = channelMerchantAccessAuthQueryContext.getMerchantAuthStatusConfig();
        try {
            // 组装参数
            Map<String,Object> data = new HashMap<>();
            data.put("agent_no",haikePayConfig.getFtpName());
            data.put("merch_no",merchantAccessInfo.getSubMchId());
            // 支付类型
            switch (authStatusConfig.getPayType()){
                // 备注 微信授权状态查询 haike 没做好，暂时不管
                case WECHAT:
                    data.put("wx_merch_no",authStatusConfig.getReportMerchantNo());
                    log.info("海科 WECHAT 商户授权状态查询 请求参数 :{}", JsonUtils.toJsonString(data));
                    String wxResponse = haikePayCommonParamsHandle.requestPostApi(HaikePayConstant.WX_MER_AUTH_QUERY_URL,data,haikePayConfig);
                    log.info("海科 WECHAT 商户授权状态查询 返回参数 :{}", wxResponse);
                    HaikeAccessMerAuthResp wxAuthResp = JSONUtil.toBean(wxResponse,HaikeAccessMerAuthResp.class);
                    if(HaikePayConstant.OK.equals(wxAuthResp.getReturn_code())){
                        if(wxAuthResp.getAuthorize_state().equals("AUTHORIZE_STATE_AUTHORIZED")){
                            authStatus = 1;
                        }else{
                            authStatus = 0;
                        }
                    }else{
                        // 海科默认已授权
                        authStatus = 1;
                    }
                    break;
                case ALIPAY:
                    data.put("ali_merch_no",authStatusConfig.getReportMerchantNo());
                    log.info("海科 ALIPAY 商户授权状态查询 请求参数 :{}", JsonUtils.toJsonString(data));
                    String aliResponse = haikePayCommonParamsHandle.requestPostApi(HaikePayConstant.ALI_MER_AUTH_QUERY_URL,data,haikePayConfig);
                    log.info("海科 ALIPAY 商户授权状态查询 返回参数 :{}", aliResponse);
                    HaikeAccessMerAuthResp aliAuthResp = JSONUtil.toBean(aliResponse,HaikeAccessMerAuthResp.class);
                    if(HaikePayConstant.OK.equals(aliAuthResp.getReturn_code())){
                        switch (aliAuthResp.getAuthorize_state()){
                            case "AUTHORIZED":
                                authStatus = 1;
                                break;
                            case "UNAUTHORIZED":
                                authStatus = 0;
                                break;
                            case "CLOSED":
                                authStatus = 2;
                                break;
                            case "SMID_NOT_EXIST":
                                authStatus = 3;
                                break;
                        }

                    }
                    break;
            }
        }
        catch (Exception e) {
            log.error("海科商户授权状态请求失败,失败信息 message: {}", e);
            channelMerchantAccessAuthQueryContext.setRemark("海科商户授权状态请求失败,失败信息" + e.getMessage());
            channelMerchantAccessAuthQueryContext.setProcessStatus(Boolean.FALSE);
        }
        return authStatus;
    }

    @Override
    public MerchantProductQueryResp productQuery(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        return null;
    }

    @Override
    public MerchantChannelSplitCallBackDto easySplitCallBackParamsParse(HttpServletRequest request, ProviderChannelConfigDO providerChannelConfigDO) {
        return null;
    }


}
